 
/* 
 * Copyright (C) Ignacio Lucas
 */
 
/*
 *  socket_utils, hecho por Ignacio Lucas
 *
 *  Utilidades para el manejo de sockets TCP.
 *
 *  Ignacio Lucas <ignacio.lucas@gmail.com>
 */
#include "common.h"
#include "socket_utils.h"

socket_t socket_tcp_create_and_connect(server_worker_job_t worker_job, char *hostname, int port, int *error)
{
	socket_t s = socket_init_with_job(AF_INET, SOCK_STREAM, IPPROTO_TCP, worker_job);
	if (s==NULL)
	{
		*error=1;
		return NULL;
	}
	*error=socket_connect(s, hostname, port);
	if (*error != 0)
	{
		logger_core_error("[socket] error conectando a host '%s', puerto %d. errno=%d, error=%d", hostname, port, socket_get_last_error(s), *error);
		// liberamos recursos y devolvemos null.
		socket_destroy(s);
		s=NULL;
	}
	
	return s;
}

socket_t socket_utils_connect(char *hostname, int port, int *error)
{
	return socket_tcp_create_and_connect(user_util_get_current_job(),hostname, port,error);
}
